package buffer

import (
	"bytes"

	"gitee.com/hasika/gotool/poolx"
)

var ins *poolx.SizePool[*bytes.Buffer]

func init() {
	ins = poolx.NewSizePool[*bytes.Buffer](func(size int) (index int) {
		return poolx.FastLog2(size)
	}, func(data *bytes.Buffer) (index int) {
		data.Reset()
		return poolx.FastLog2(data.Cap())
	}, func(size int) *bytes.Buffer {
		return bytes.NewBuffer(make([]byte, 0, size))
	})
}

func Allocate(size int) *bytes.Buffer {
	return ins.Get(size)
}

func Return(data *bytes.Buffer) {
	ins.Put(data)
}
